//
//  SettleManageVC.swift
//  kxf
//
//  Created by Elavatine on 2024/10/12.
//

import MCToast

class SettleManageVC: WHBaseViewVC {
    
    var typeDict = NSDictionary()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
        
        if UserModel.shared.settleCycle.count > 0 {
            typeDict = ["type":"\(UserModel.shared.settleCycle)","name":"\(UserModel.shared.settleCycleName)"]
        }
    }
    lazy var typeVm: SettleManageTypeVM = {
        let vm = SettleManageTypeVM.init(frame: CGRect.init(x: 0, y: getNavigationBarHeight()+kFitWidth(8), width: 0, height: 0))
        
        vm.tapBlock = {(dict)in
            DLLog(message: "\(dict)")
            self.typeDict = dict
            if dict["type"]as? String  ?? "" == "4"{
                self.timeVm.isHidden = false
            }else{
                self.timeVm.isHidden = true
            }
        }
//        vm.initMsg()
        return vm
    }()
    lazy var timeVm: SettleManageTimeVM = {
        let vm = SettleManageTimeVM.init(frame: CGRect.init(x: 0, y: self.typeVm.frame.maxY, width: 0, height: 0))
        vm.isHidden = true
        return vm
    }()
    lazy var nextBtn : UIButton = {
        let btn = UIButton()
        btn.frame = CGRect.init(x: kFitWidth(31), y: SCREEN_HEIGHT-getTabbarHeight()-kFitWidth(40), width: kFitWidth(316), height: kFitWidth(48))
        btn.setTitle("确认", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.setBackgroundImage(UIImage.init(named: "button_theme_bg"), for: .normal)
        btn.layer.cornerRadius = kFitWidth(24)
        btn.clipsToBounds = true
        btn.titleLabel?.font = .systemFont(ofSize: 16, weight: .regular)
        btn.backgroundColor = .clear
        
        btn.addTarget(self, action: #selector(submitAction), for: .touchUpInside)
        
        return btn
    }()
}

extension SettleManageVC{
    @objc func submitAction() {
        if typeDict["type"]as? String ?? "" == ""{
            MCToast.mc_text("请选择结算周期")
            return
        }
        sendSetSettleTypeRequest()
    }
    func sendSetSettleTypeRequest() {
//        var param = ["settleCycle":"\(typeDict["type"]as? String ?? "")"]
        
        var urlString = "\(URL_settleType_update)?settleCycle=\(typeDict["type"]as? String ?? "")"
        if typeDict["type"]as? String ?? "" == "4"{
            let hourRow = self.timeVm.timePickerView.selectedRow(inComponent: 0)
            let minuteRow = self.timeVm.timePickerView.selectedRow(inComponent: 1)
            
            let hour = self.timeVm.hourTimeArray[hourRow]as? String ?? ""
            let minute = self.timeVm.minutTimeArray[minuteRow]as? String ?? ""
            
            urlString = "\(URL_settleType_update)?settleCycle=\(typeDict["type"]as? String ?? "")&auditTime=\(hour):\(minute)"
            
//            param = ["settleCycle":"\(typeDict["type"]as? String ?? "")",
//                     "auditTime":"\(hour):\(minute)"]
        }
        WHNetworkUtil.shareManager().POST(urlString: urlString, parameters: nil,isNeedToast: true,vc: self) { responseObject in
            DLLog(message: "sendSetSettleTypeRequest:\(responseObject)")
            self.presentAlertVcNoAction(title: "结算周期修改成功", viewController: self)
        }
//        WHNetworkUtil.shareManager().GET(urlString: urlString,vc: self) { responseObject in
//            DLLog(message: "sendSetSettleTypeRequest:\(responseObject)")
//            if responseObject["code"]as? Int ?? 0 == 200{
//                self.presentAlertVcNoAction(title: "结算周期修改成功", viewController: self)
//            }else{
//                self.presentAlertVcNoAction(title: "\(responseObject["message"]as? String ?? "请求异常，请联系代理商")", viewController: self)
//            }
//        }
    }
}
extension SettleManageVC{
    func initUI() {
        initNavi(titleStr: "结算管理")
        view.addSubview(typeVm)
        view.addSubview(timeVm)
        
        view.addSubview(nextBtn)
    }
}
